/*
 * @Author: Du.M
 * @Date: 2021-08-20 14:38:48
 * @LastEditTime: 2021-08-23 09:26:21
 * @FilePath: \express_basics\createNum.js
 */
//定时任务
const mysql = require('mysql');
const moment = require('moment')
const mysqlConfig = {
    host: 'localhost', // 主机名称，一般是本机
    port: '3306', // 数据库的端口号，如果不设置，默认是3306
    user: 'root', // 创建数据库时设置用户名
    password: 'Dm980227', // 创建数据库时设置的密码
    database: 'renderhouse', // 创建的数据库
    connectTimeout: 5000 // 连接超时
}// 连接mysql
const connect = () => {
    const {
        host,
        user,
        password,
        database
    } = mysqlConfig;
    return mysql.createConnection({
        host,
        user,
        password,
        database
    })
}
// 新建查询连接
const querySql = (sql) => {
    const connection = connect();
    return new Promise((resolve, reject) => {
        try {
            connection.query(sql, (err, res) => {
            if (err) {
              reject(err);
            } else {
              resolve(res);
            }
          })
        } catch (e) {
          reject(e);
        } finally {
          //释放连接
          connection.end();
        }
      })
    

}
const compare = (property) =>{
    return function (a, b) {
        var value1 = a[property];
        var value2 = b[property];
        return value2 - value1;
    }
}
let sl_before = []
let sl_after = []
let ul_before = []
let ul_after = []
const createSuperLotto = () =>{
    const originBefore = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
        , 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
    const originAfter = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    
    let before = []
    let after = []
    for (i = 0; i < 100000; i++) {
        let tempBefore = [...originBefore]
        let tempAfter = [...originAfter]
        const random = Math.floor(Math.random() * 35);
        before.push(tempBefore[random])
        const random_copy = Math.floor(Math.random() * 12);
        after.push(tempAfter[random_copy])

    }
    before.sort()
    after.sort()
    let resB = [];
    let resA = [];
    for (let i = 0; i < before.length;) {
        let count = 0;
        for (var j = i; j < before.length; j++) {
            if (before[i] == before[j]) {
                count++;
            }
        }
        resB.push([before[i], count]);
        i += count;
    }
    for (let i = 0; i < after.length;) {
        let count = 0;
        for (var j = i; j < after.length; j++) {
            if (after[i] == after[j]) {
                count++;
            }
        }
        resA.push([after[i], count]);
        i += count;
    }
    resB.sort(compare(1))
    resA.sort(compare(1))
   
    for(let i = 0;i < 7 ;i++){
        sl_before.push(resB[i][0])
    }
    for(let i = 0;i < 3 ; i++){
        sl_after.push(resA[i][0])
    }
}
const createUnionLotto = () =>{
    const originBefore = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
        , 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
    const originAfter = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 , 13 , 14 , 15 , 16 ]
    
    let before = []
    let after = []
    for (i = 0; i < 10000000; i++) {
        let tempBefore = [...originBefore]
        let tempAfter = [...originAfter]
        const random = Math.floor(Math.random() * 33);
        before.push(tempBefore[random])
        const random_copy = Math.floor(Math.random() * 16);
        after.push(tempAfter[random_copy])

    }
    before.sort()
    after.sort()
    let resB = [];
    let resA = [];
    for (let i = 0; i < before.length;) {
        let count = 0;
        for (var j = i; j < before.length; j++) {
            if (before[i] == before[j]) {
                count++;
            }
        }
        resB.push([before[i], count]);
        i += count;
    }
    for (let i = 0; i < after.length;) {
        let count = 0;
        for (var j = i; j < after.length; j++) {
            if (after[i] == after[j]) {
                count++;
            }
        }
        resA.push([after[i], count]);
        i += count;
    }
    resB.sort(compare(1))
    resA.sort(compare(1))
   
    for(let i = 0;i < 8 ;i++){
        ul_before.push(resB[i][0])
    }
    for(let i = 0;i < 2 ; i++){
        ul_after.push(resA[i][0])
    }
}
createSuperLotto()
createUnionLotto()
console.log(sl_before,sl_after,ul_before,ul_after)
const time = moment(new Date()).format('YYYY-MM-DD');
const sql = `insert into nums(date,sl_before,sl_after,ul_before,ul_after) value (${time},"${sl_before}","${sl_after}","${ul_before}","${ul_after}")`
querySql(sql).then(result =>{
    console.log("成功")
})